package com.xdqzy.baseline;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GeneratorServiceEntity {

	@Test
    public void generateCode() {
		String packageName = "com.xdqzy.baseline.common";
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
		generateByTables(serviceNameStartWithI, packageName, "t_sys_mark");
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        config.setEnableCache(false);
        config.setSwagger2(true);
        config.setEntityName("%sModel");
		String dbUrl = "jdbc:mysql://118.126.65.111:3306/ctbc?useSSL=false&serverTimezone=UTC";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
		        .setUsername("root").setPassword("QAZ123WSX").setDriverName("com.mysql.cj.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
		        .setCapitalMode(false)
				.setRestControllerStyle(true)
                .setEntityLombokModel(true)
		        .setNaming(NamingStrategy.underline_to_camel).setColumnNaming(NamingStrategy.underline_to_camel)
		        .setInclude(tableNames).setTablePrefix("t_");// 修改替换成你需要的表名，多个表名传数组
        config.setActiveRecord(false)
		        .setAuthor("qss")
				.setOutputDir("D:\\wb\\codeGen")
                .setFileOverride(true).setDateType(DateType.ONLY_DATE);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }

		// 自定义 xxList.jsp 生成
		List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
//		focList.add(new FileOutConfig("/templates/list.jsp.vm") {
		//
		// @Override
		// public String outputFile(TableInfo tableInfo) {
		// // 自定义输入文件名称
		// return "E://codeGen//" + tableInfo.getEntityName() + ".jsp";
		// }
		// });

		// 注入自定义配置，可以在 VM 中使用 cfg.abc 【可无】
		InjectionConfig cfg = new InjectionConfig() {

			@Override
			public void initMap() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
				this.setMap(map);
			}
		};
		cfg.setFileOutConfigList(focList);
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
		                                .setEntity("model").setXml("mapper")
				).setCfg(cfg).execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }
}
